package com.sooth.system.datasource.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.dream.system.config.Page;
import com.sooth.common.core.domain.R;
import com.sooth.common.dream.model.PageModel;
import com.sooth.common.log.annotation.Log;
import com.sooth.common.web.core.BaseController;
import com.sooth.system.datasource.service.ISysDatasourceService;
import com.sooth.system.datasource.view.SysDataSourceBo;
import com.sooth.system.datasource.view.SysDataSourceDto;
import com.sooth.system.datasource.view.SysDataSourceVo;
import com.sooth.system.datasource.view.TableColumnVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 数据源信息操作处理
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/datasource")
public class SysDatasourceController extends BaseController {

    private final ISysDatasourceService datasourceService;

    /**
     * 获取数据源列表
     */
    @SaCheckPermission("system:datasource:query")
    @GetMapping("/list")
    public R<Page<SysDataSourceVo>> list(SysDataSourceDto dataSource, PageModel pageModel) {
        return R.ok(datasourceService.selectPage(dataSource, pageModel));
    }

    /**
     * 获取所有数据源
     */
    @SaCheckPermission("system:datasource:query")
    @GetMapping("/listAll")
    public R<List<SysDataSourceVo>> listAll(SysDataSourceDto dataSource) {
        return R.ok(datasourceService.selectList(dataSource));
    }

    /**
     * 根据数据源编号获取详细信息
     *
     * @param id 数据源ID
     */
    @SaCheckPermission("system:datasource:query")
    @GetMapping(value = "/query")
    public R<SysDataSourceBo> query(@RequestParam Long id) {
        return R.ok(datasourceService.selectById(id));
    }

    /**
     * 新增数据源
     */
    @SaCheckPermission("system:datasource:add")
    @Log(title = "数据源管理", message = "新增数据源")
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysDataSourceBo datasource) {
        return toAjax(datasourceService.insert(datasource));
    }

    /**
     * 修改数据源
     */
    @SaCheckPermission("system:datasource:edit")
    @Log(title = "数据源管理", message = "修改数据源")
    @PutMapping("/edit")
    public R<Void> edit(@RequestBody SysDataSourceBo datasource) {
        return toAjax(datasourceService.updateById(datasource));
    }

    /**
     * 删除数据源
     *
     * @param id 数据源ID
     */
    @SaCheckPermission("system:datasource:remove")
    @Log(title = "数据源管理", message = "删除数据源")
    @DeleteMapping("/remove")
    public R<Void> remove(@RequestParam Long id) {
        return toAjax(datasourceService.deleteById(id));
    }

    /**
     * 测试数据源连接
     */
    @SaCheckPermission("system:datasource:test")
    @Log(title = "数据源管理", message = "测试数据源连接")
    @PostMapping("/test")
    public R<Void> test(@RequestBody SysDataSourceBo datasource) {
        datasourceService.test(datasource);
        return R.ok("连接成功");
    }

    @SaCheckPermission("system:database:query")
    @ResponseBody
    @GetMapping(value = "/listTable")
    public R<List<Map<String, String>>> listTable(@RequestParam Long datasourceId) {
        List<Map<String, String>> tableList = datasourceService.listTable(datasourceId);
        return R.ok(tableList);
    }

    @SaCheckPermission("system:database:query")
    @ResponseBody
    @GetMapping(value = "/listField")
    public R<List<TableColumnVo>> listField(@RequestParam Long datasourceId, @RequestParam String table) {
        List<TableColumnVo> fieldList = datasourceService.listField(datasourceId, table);
        return R.ok(fieldList);
    }

    @SaCheckPermission("system:database:query")
    @ResponseBody
    @GetMapping(value = "/listPrimaryKeys")
    public R<List<String>> listPrimaryKeys(@RequestParam Long datasourceId, @RequestParam String table) {
        List<String> primaryList = datasourceService.listPrimaryKeys(datasourceId, table);
        return R.ok(primaryList);
    }
}
